# -*- coding: utf-8 -*-
# File : configure_service.py
# Author: taoyahui
# Date : 2021/7/11
from fabric import Connection
import json
from utils import common_util, mysql_util
import results
import os
import time
import configparser

cf = configparser.ConfigParser()
cf.read(os.path.join(common_util.get_root_dir(),"config.ini"))

secs = cf.sections()
ip = cf.get("target-conf", "host")
port = cf.get("target-conf", "port")
username = cf.get("target-conf", "username")
password = cf.get("target-conf", "password")
from tasks import sys_configure


# 获取系统的信息(cpu、ram、disk等等)
def get_system():
    """
    获取系统的信息(cpu、ram、disk等等)
    :return:
    """
    sys_configure.get_system_configure()

    query_sql = "select * from sys_conf order by id desc limit 1"
    res = mysql_util.exec_sql_new(query_sql)
    if len(res) == 0:
        return results.sys_configure_empty_error
    d = {
        'cpu_used': res[0][1],
        'memory_used': res[0][2],
        'rom_used': res[0][3],
        'rom_remaind': res[0][4],
        'net_card': res[0][5],
        'tx_in_speed': res[0][6],
        'tx_out_speed': res[0][7]
    }
    response  = {
        'code':200,
        'data': d
    }
    return response



def remote_source_configure():
    """
    生成远程源的配置文件
    :return:
    """
    root_dir = common_util.get_root_dir()
    c = Connection(f"{username}@{ip}", connect_kwargs={'password': password})
    c.run('mkdir -p /root/source_dir')
    c.put(os.path.join(root_dir, 'scripts', 'ClientFtpConf.sh'), '/root/source_dir')
    c.put(os.path.join(root_dir, 'scripts', 'ftp.repo.tem'), '/root/source_dir')
    with c.cd('/root/source_dir'):
        c.run('bash ClientFtpConf.sh')
    c.get('/root/source_dir/ftp.repo', os.path.join(root_dir, 'scripts', 'ftp.repo'))

if __name__ == '__main__':
    pass